package com.jsj.service.impl;

import com.jsj.mapper.LabDeviceMapper;
import com.jsj.pojo.Device;
import com.jsj.pojo.LabDevice;
import com.jsj.pojo.Laboratory;
import com.jsj.service.DeviceService;
import com.jsj.service.LabDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LabDeviceServiceImpl implements LabDeviceService {

    @Autowired
    LabDeviceMapper labDeviceMapper;
    @Autowired
    DeviceService deviceService;

    List labDevList = new ArrayList();

    @Override
    public List<Device> queryDeviceIdByLabId(int labId) {
        labDevList.clear();
        //查出该实验室lab_id对应的所有的设备device_id
        List<LabDevice> deviceIdList = labDeviceMapper.queryDeviceIdByLabId(labId);
        System.out.println(deviceIdList);
        //遍历查出每个device_id对应的具体设备信息

        for (LabDevice laboratory : deviceIdList) {

            int deviceId = laboratory.getDeviceId();
            Device device = deviceService.queryByDeviceId(deviceId);
            labDevList.add(device);
        }
        return labDevList;
    }

    @Override
    public List addDeviceToLab(int labId, int deviceId) {
        //检测是否是重复数据
        //查出该实验室lab_id对应的所有的设备device_id
        List<LabDevice> deviceIdList = labDeviceMapper.queryDeviceIdByLabId(labId);
        ArrayList list = new ArrayList();
        for (LabDevice device : deviceIdList) {
            //for如果传过来的是多个deviceId
            if (device.getDeviceId()==deviceId){
                list.add(deviceId);
            }
        }
        if (list!=null&&!list.isEmpty()){
            return list;
        }

        Boolean aBoolean = labDeviceMapper.addDeviceToLab(labId, deviceId);
        list.add(aBoolean);
        return list;
    }


    @Override
    public Boolean delLabDeviceById(int labId, int deviceId) {

        Boolean aBoolean = labDeviceMapper.delLabDeviceById(labId, deviceId);
        return aBoolean;
    }

    @Override
    public List<Device> queryLabDeviceByName(String deviceName) {

        List<Device> devices = labDeviceMapper.queryLabDeviceByName(labDevList,deviceName);

        return devices;
    }


}
